<template>
  <div>
    <el-card style="width: 40%; margin: 10px">
      <el-form ref="form" :model="form" label-width="80px" :rules="rules">
        <el-form-item label="账号">
          <el-input v-model="form.account" disabled></el-input>
        </el-form-item>
        <el-form-item label="姓名" >
          <el-input v-model="form.name" disabled></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" show-password></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="rpassword">
          <el-input v-model="form.rpassword" show-password></el-input>
        </el-form-item>
      </el-form>
      <div style="text-align: center">
        <el-button type="primary" @click="update">保存</el-button>
      </div>
    </el-card>

  </div>
</template>

<script>
import request from "../../utils/request";
export default {
  name: "Person",
  data() {
     var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.form.password) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
  }
    return {
         form: {},
         rules:{
           password:[
             {required: true, message: '密码不能为空', trigger: 'blur'},
            { min: 5, max: 18, message: '请输入5到18位密码', trigger: 'blur' }
           ],
           rpassword:[
             { validator: validatePass2, trigger: 'blur' }
           ]
         }
      }
  },
created() {
    let str = sessionStorage.getItem("user") || "{}"
    this.form = JSON.parse(str)
    this.form.password = null
  },
  methods:{

     update() {
       console.log(this.form);
       this.$refs.form.validate((valid) => {
          if (valid) {
              request.put("/user/resetpwd", this.form).then(res => {
        console.log(res)
        if (res.code === '0') {
          this.$message({
            type: "success",
            message: "修改成功"
          })
          this.$router.push("/login");
          sessionStorage.removeItem("user")
          // sessionStorage.setItem("user", JSON.stringify(this.form))
          // // 触发Layout更新用户信息
          // this.$emit("userInfo")
        } else {
          this.$message({
            type: "error",
            message: res.msg
          })
        }
      })
          }
       })
    
    },
    
  }
 
}
 
</script>

